#region License, Terms and Conditions
//
// Prawks - Copyright (c) 2007 Atif Aziz. All rights reserved.
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the Free
// Software Foundation; either version 2.1 of the License, or (at your option)
// any later version.
//
// This library is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
// details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this library; if not, write to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
//
#endregion

using System;
using Utilities;

using StringWriter = System.IO.StringWriter;

internal sealed class ProgramOptions
{
    [ DefaultCommandLineArgument(CommandLineArgumentType.Required) ]
    public string Database = null;
    [ CommandLineArgument(CommandLineArgumentType.AtMostOnce, ShortName = "s") ]
    public string Server = ".";
    [ CommandLineArgument(CommandLineArgumentType.AtMostOnce, LongName = "TrustedConnection", ShortName = "E") ]
    public bool UseTrustedConnection = true;
    [ CommandLineArgument(CommandLineArgumentType.AtMostOnce, ShortName = "uid") ]
    public string UserID = string.Empty;
    [ CommandLineArgument(CommandLineArgumentType.AtMostOnce, ShortName = "pwd") ]
    public string Password = string.Empty;
    public string Language = "C#";
    [ CommandLineArgument(CommandLineArgumentType.AtMostOnce, ShortName = "ns") ]
    public string Namespace = string.Empty;
    public string TypeName = string.Empty;
    public string BaseTypeName = string.Empty;
    [ CommandLineArgument(CommandLineArgumentType.AtMostOnce, LongName = "OutputFile") ]
    public string OutputFileName = string.Empty;
    [ CommandLineArgument(CommandLineArgumentType.AtMostOnce, LongName = "NullableTypes", ShortName = "nulls") ]
    public bool UseNullableTypes = false;
    [CommandLineArgument(CommandLineArgumentType.AtMostOnce, LongName = "PartialClass", ShortName = "pc")]
    public bool MarkClassPartial = false;
    public string Factory = "CreateInstance";
    [ CommandLineArgument(CommandLineArgumentType.MultipleUnique, LongName = "Procedure", ShortName = "sp") ]
    public string[] Procedures = new string[0];
    [ CommandLineArgument(CommandLineArgumentType.AtMostOnce, ShortName = "gc") ]
    public bool GenerateCode = true;
    [ CommandLineArgument(CommandLineArgumentType.AtMostOnce, ShortName = "cc") ]
    public bool CompileCode = false;
    [ CommandLineArgument(CommandLineArgumentType.MultipleUnique, LongName = "AssemblyRef", ShortName = "r") ]
    public string[] AssemblyReferences = new string[0];
    [ CommandLineArgument(CommandLineArgumentType.AtMostOnce, LongName = "OutputAssembly", ShortName = "oa") ]
    public string OutputAssemblyName = string.Empty;

    public bool ShouldGenerateFactory
    {
        get { return Factory != NoFactoryValue; }
    }

    public string DatabaseTitle
    {
        get
        {
            if (Database == null || Database.Length == 0)
                return Database;

            return Char.ToUpper(Database[0]) + Database.Substring(1);
        }
    }

    public const string NoFactoryValue = "-";

    public static ProgramOptions ParseFromArgs(string[] args)
    {
        ProgramOptions options = new ProgramOptions();
        StringWriter parseErrorWriter = new StringWriter();

        if (!Utility.ParseCommandLineArguments(args, options, new ErrorReporter(parseErrorWriter.WriteLine)))
            throw new ProgramException(parseErrorWriter.GetStringBuilder().ToString());

        return options;
    }

    public static void ShowCommandLineUsage()
    {
        Utility.WriteCommandLineArgumentsUsage(typeof(ProgramOptions));
    }
}
